package com.qj.yantaishan.common.utils;

import lombok.SneakyThrows;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 时间操作工具类
 */
public class TimeMyUtil {
    /**
     * 根据时间挫和差的日期得出当天的开始时间和结束时间
     *
     * @param time         当前时间
     * @param calculateDay 要运算的时间  +xx天 或-xx天
     * @return
     */
    public static Map<String, Long> getStartAndEndTime(Long time, Integer calculateDay) {

        Map<String, Long> longMap = new HashMap<>();
        //时间操作
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time * 1000));
        //计算时间
        calendar.add(Calendar.DAY_OF_MONTH, calculateDay);
        //得出运算后的日期
        Date startDate = calendar.getTime();
        Date endDate = calendar.getTime();
        //开始时间
        startDate.setHours(0);
        startDate.setMinutes(0);
        startDate.setSeconds(0);
        //结束时间
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        longMap.put("startTime", startDate.getTime() / 1000);
        longMap.put("endTime", endDate.getTime() / 1000);
        return longMap;
    }

    @SneakyThrows
    public static Map<String, Long> caleMount(String date, Integer caleMount) {
        Map<String, Long> longMap = new HashMap<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-dd-MM");
        Date time = dateFormat.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.MONTH, caleMount);
        return getMountStartAndEndTime(calendar.getTime().getTime() / 1000);
    }

    public static Map<String, Long> getMountStartAndEndTime(Long time) {

        Map<String, Long> longMap = new HashMap<>();
        //时间操作
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time * 1000));
        //得出运算后的日期
        Date startDate = calendar.getTime();
        Date endDate = calendar.getTime();
        //开始时间
        startDate.setDate(1);
        startDate.setHours(0);
        startDate.setMinutes(0);
        startDate.setSeconds(0);
        //结束时间
        endDate.setDate(31);
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        longMap.put("startTime", startDate.getTime() / 1000);
        longMap.put("endTime", endDate.getTime() / 1000);
        return longMap;
    }
}
